<template>


	<view class="recording">
		<view class="state">
			<text v-if="!playValue">{{recordingStateText}}</text>
			<custom-button-voice-play v-else :local-play-value="playValue">

			</custom-button-voice-play>

		</view>

		<view>
			<fui-button background="#F2F3FF" color="#0052D9" width="200rpx" height="64rpx" @touchend="touchend"
				@click="onClick" @longpress="onLongpress">
				{{buttonStateText}}
			</fui-button>

		</view>
	</view>

</template>

<script>
	import {
		FileApi
	} from '../../../api/file';
	// import {
	// 	wx_Voice
	// } from '../../../utils/wechat';
	import {
		mapActions
	} from 'pinia'
	import {
		useWx
	} from '../../../store/modules/wx';
	export default {
		props: {
			playValue: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				btnText: '按住开始录音',
				loading: null,
				islongPress: false, //长按记录变量
				voice: null,
				// 按钮状态文本
				buttonStateText: '按住开始录音',
				// 按钮状态颜色
				// 录音状态文本
				recordingStateText: '',
			}
		},

		methods: {
			...mapActions(useWx, ['injectSdk', 'wxHandle']),

			// 长按
			onLongpress() {
				const the = this
				the.injectSdk(() => {


					the.wxHandle().startRecord({
						success: function() {
							the.$emit('change', '')
							the.buttonStateText = '录音中...'
							the.recordingStateText = "录音中完成后松手"
							console.log('准备录音');
						},
						cancel: function() {
							the.buttonStateText = '录音'
							the.recordingStateText = ""
							console.log("用户拒绝授权录音");
							// uni.showModal({
							// 	showCancel:false,
							// 	content:'录音功能无法使用'
							// })
						},
					})
				})

			},

			// 长按结束
			touchend() {

				console.log('touchend');
				const the = this

				uni.hideLoading()
				this.wxHandle().stopRecord({
					success: (res) => {
						console.log('res', res);
						the.buttonStateText = '重录'
						the.recordingStateText = "录音完成"
						the.$emit('change', res.localId)
						// this.wxHandle().uploadVoice({
						// 	localId: res.localId, // 需要上传的音频的本地ID，由stopRecord接口获得
						// 	isShowProgressTips: 1, // 默认为1，显示进度提示
						// 	success: async function(res) {


						// 		const serverId = res.serverId; // 返回音频的服务器端ID
						// 		console.log(serverId, 'serverId');
						// 		the.localId = res.localId
						// 		the.buttonStateText = '重录'
						// 		the.recordingStateText = "录音完成"
						// 		const g = await FileApi.upWechatVoice(serverId)
						// 		// console.log(g, '---长按结束-----', );
						// 		the.$emit('change', g.data)


						// 	}
						// });
					}
				})

				setTimeout(() => {
					this.islongPress = false
				}, 200)
			}


		}
	}
</script>

<style lang="scss" scoped>
	.recording {
		background: #ffffff;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.state {
		font-size: 28rpx;
		font-weight: 400;
		color: #e37318;
		flex: 1;
	}
</style>